package kr.pe.smallhope.mobismap.utils;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

/**
 * @author Lee Sanghoon
 * 
 */
public class HttpUtil {
	public static String request(String url, String responseCharsetName) {
//		System.err.println(url);

		HttpClient httpClient = new HttpClient();
		HttpMethod method = new GetMethod(url);

		try {
			int statusCode = httpClient.executeMethod(method);

			if (statusCode == HttpStatus.SC_OK) {
				// String response = method.getResponseBodyAsString();

				String response = new String(method.getResponseBody(), responseCharsetName);
//				System.err.println(response);
				return response;
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			method.releaseConnection();
		}

		return null;
	}

	public static String request(String url) {
		return request(url, "UTF-8");
	}
}
